//
//  DWTabBarView.swift
//  DWSwift
//
//  Created by GDL on 2025/6/20.
//

import SwiftUI
import SwiftfulRouting
struct DWTabBarView: View {
    @State private var tabs: [DWTabItem] = [
        DWTabItem(name: "Home", icon: "house"),
        DWTabItem(name: "Order", icon: "magnifyingglass"),
        DWTabItem(name: "In", icon: "plus.circle"),
//        DWTabItem(name: "Setting", icon: "magnifyingglass"),
//        DWTabItem(name: "Plus", icon: "plus.circle")
    ]

    @State private var availableTabs: [DWTabItem] = [
        DWTabItem(name: "Notifications", icon: "bell"),
        DWTabItem(name: "Downloads", icon: "arrow.down"),
        DWTabItem(name: "Profile", icon: "person"),
        DWTabItem(name: "Settings", icon: "gear"),
        DWTabItem(name: "Favorites", icon: "heart"),
        DWTabItem(name: "Messages", icon: "message")
    ]

    @State private var selectedTab = 0
    @State private var showPopup = false

    init() {
        // 基础方案：设置背景色
        UITabBar.appearance().backgroundColor = UIColor(.gray) // 背景色[3](@ref)
        // 进阶方案：精细控制（iOS 13+）
        let appearance = UITabBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UIColor(Color.white) // 自定义背景色
        UITabBar.appearance().standardAppearance = appearance
        UITabBar.appearance().scrollEdgeAppearance = appearance // 适配滚动边缘[3,6](@ref)
    }
    
    var body: some View {
        VStack {
            HStack {
                TabView(selection: $selectedTab) {
                    ForEach(tabs) {tab in
                        DWTabViewContent(tab: tab, selectedTab: $selectedTab, tabs: $tabs){_ in
                            let index = tabs.firstIndex(where: { $0.id == tab.id })!
                            switch index {
                            case 0: DWHomeView().navigationBarHidden(true)
                                case 1: ContentView().navigationBarHidden(true)
                                case 2: DWProfileRefresherView().navigationBarHidden(true)
                            default:
                                DWHomeView()
                            }
                        }.tag(tabs.firstIndex(of: tab) ?? 0)
                            .onChange(of: selectedTab) { _ in
                                
//                                if selectedTab == tabs.count - 1 {
//                                    withAnimation {
//                                        showPopup = true
//                                        selectedTab = 0
//                                    }
//                                }
                            }
                    }
                }
            }

            if showPopup {
                DWPopupView(showPopup: $showPopup, availableTabs: $availableTabs, tabs: $tabs)
                    .transition(.opacity)
            }
        }
        .background(Color.red)
    }
}

struct DWTabItem: Identifiable, Equatable {
    let id = UUID()
    let name: String
    let icon: String
}

struct DWTabBarView_Previews: PreviewProvider {
    static var previews: some View {
        DWTabBarView()
    }
}

